package cn.lf.android.lawfirm.outgoingregistration.controller;


import android.content.Context;
import android.util.Pair;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

import static android.R.id.list;

/**
 * 添加
 */
public class AddOutGoingConroller extends BaseController {
    AddOutGoingCallback mAddOutGoingCallback;
    private Context context;

    public AddOutGoingConroller(AddOutGoingCallback callback, Context context) {
        this.mAddOutGoingCallback = callback;
        this.context = context;
    }


    /**
     * @param Id            外出记录编号（必填）
     * @param startTime     格式: "2017-07-13 13:00",--开始时间
     * @param endTime       格式: "2017-05-12 22:00",--结束时间
     * @param Destination   外出地点（必填）
     * @param LsysOutReason 外出事项（必填）
     */
    public void onAddOutGoing(String Id, String startTime, String endTime, String Destination, String LsysOutReason) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("Id", Id);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("Destination", Destination);
        params.put("LsysOutReason", LsysOutReason);


        DebugLog.e("Id===" + Id);
        DebugLog.e("startTime===" + startTime);
        DebugLog.e("endTime===" + endTime);
        DebugLog.e("Destination===" + Destination);
        DebugLog.e("LsysOutReason===" + LsysOutReason);

        new MyOkHttpClient().doPost(AppConfig.LSCaseApproveRecordList_Create, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("添加外出登记===" + response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mAddOutGoingCallback.onAddOutGoingSuccessed("");
                            } else {
                                mAddOutGoingCallback
                                        .onAddOutGoingFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("添加外出登记失败===" + errorMsg);
                        mAddOutGoingCallback.onAddOutGoingFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface AddOutGoingCallback {
        public void onAddOutGoingSuccessed(String str);

        public void onAddOutGoingFailed();

    }

}
